<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName" v-loading="loading">
          <el-tab-pane label="角色管理" name="roles">
            <el-button type="primary" @click="showDialog=true">+ 新增角色</el-button>
            <el-table :data="ruleList" border stripe class="myTable">
              <el-table-column label="序号" type="index" width="140px" :index="indexMethod" />
              <el-table-column label="角色名称" prop="name" width="200px" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <el-button type="success" size="mini">分配权限</el-button>
                  <el-button type="primary" size="mini" @click="editRole(row.id)">编辑</el-button>
                  <el-button type="danger" size="mini" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              :current-page="page"
              :page-sizes="[2, 4, 8, 10]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyForm.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyForm.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyForm.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyForm.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog :title="showTitle" :visible="showDialog" :close-on-click-modal="false" @close="handleClose">
        <el-form ref="roleForm" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="form.description" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>

        <template #footer>
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="clickSubmit">确认</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { reqGetRoleList, reqDeleteRole, reqAddRole, reqGetRoleDetail, reqUpdateRole } from '@/api/setting'
import { reqGetCompanyById } from '@/api/company'
import { mapState } from 'vuex'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'roles',
      ruleList: [],
      page: 1,
      pageSize: 4,
      total: 0,
      loading: false,
      // 控制弹框的显示与隐藏
      showDialog: false,
      companyForm: {},
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: ['blur', 'change'] }]
      }
    }
  },
  // 控制弹框标题
  computed: {
    showTitle() {
      return this.form.id ? '编辑角色' : '新增角色'
    },
    ...mapState('user', ['userInfo'])
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      this.loading = true
      const { data: res } = await reqGetRoleList(this.page, this.pageSize)
      // console.log(res)
      // console.log(this.page, this.pageSize)
      this.ruleList = res.rows
      this.total = res.total
      this.loading = false
    },
    // 页码最大条数改变时触发
    handleSizeChange(newSize) {
      this.pageSize = newSize
      this.getRoleList()
    },
    // 页码改变时触发
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.page = newPage
      this.getRoleList()
    },
    // 自定义序列号
    indexMethod(index) {
      return (this.page - 1) * (this.pageSize) + index + 1
    },
    // 删除角色
    async delRole(id) {
      const res = await this.$confirm('您确认删除角色吗', '温馨提示').catch(error => error)
      if (res === 'cancel') return
      await reqDeleteRole(id)
      this.$message.success('删除角色成功')
      if (this.ruleList.length === 1 && this.page > 1) this.page--
      this.getRoleList()
    },
    // 关闭弹框
    handleClose() {
      this.showDialog = false
      this.$refs.roleForm.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    // 提交 角色列表
    clickSubmit() {
      this.$refs.roleForm.validate(async valid => {
        if (!valid) return
        if (this.form.id) {
          await reqUpdateRole(this.form)
          this.$message.success('更新角色成功')
        } else {
          await reqAddRole(this.form)
          this.$message.success('添加角色成功')
        }

        this.handleClose()
        this.getRoleList()
      })
    },
    // 编辑角色
    async editRole(id) {
      const res = await reqGetRoleDetail(id)
      // console.log(res)
      this.form = res.data
      this.showDialog = true
    },
    // 公司信息
    async getCompanyInfo() {
      const res = await reqGetCompanyById(this.userInfo.companyId)
      // console.log(res)
      this.companyForm = res.data
    }
  }
}
</script>

<style scoped lang="scss">
.myTable{
  margin: 15px 0;
}
</style>
